//
//  GYDPopViewController.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/17.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit
import SnapKit

public enum PopType : Int {
    case PopLeft
    case PopCenter
    case PopRight
}
public enum PopDataSoureType : Int {
    case OnlyTitle
    case TitleAndImage
    case OnlyImage
    case OnlyHome
}


class GYDPopViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var popType : PopType = PopType.PopCenter
    private var tableView : UITableView?
    var dataSoure : [AnyObject]?
    var dataSoureType : PopDataSoureType?
    var selectedIdex: Int = 0
    /// 模态动画管理者
    private var manager : GYDPopPresentationManager?
    /// 消失回调
    var dissmissBlock : (() -> ())?
    var popSelectedBlock : ((index : Int) -> Void)?
    
    
    init(frame:CGRect) {
        super.init(nibName: nil, bundle: nil)
        let manager = GYDPopPresentationManager()
        self.manager = manager
        manager.dissmissBlock = {[unowned self] () -> Void in
            if let tempDissmissBlcok = self.dissmissBlock {
                tempDissmissBlcok()
            }
        }
        manager.presentFrame = frame
        self.transitioningDelegate = manager
        self.modalPresentationStyle = UIModalPresentationStyle.Custom
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.clearColor()
        self.addBlackView()
        self.createTableView()
        self.createBottomView()
    }
    private func addBlackView() -> Void {
        let imageView = UIImageView()
        var imageName = "popover_background"
        if self.popType == PopType.PopLeft {
            imageName = "popover_background_left"
        }else if self.popType == PopType.PopRight {
            imageName = "popover_background_right"
        }
        
        imageView.image = UIImage.resizedImage(imageName)
        self.view.addSubview(imageView)
        imageView.snp_makeConstraints { (make) in
            make.left.equalTo(0.0)
            make.bottom.equalTo(0.0)
            make.right.equalTo(0.0)
            make.top.equalTo(0.0)
        }
    }
    
    private func createTableView() -> Void {
        let tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
        self.view.addSubview(tableView)
        var bottom = -4
        if self.dataSoureType == PopDataSoureType.OnlyHome {
            bottom = -48
        }
        tableView.snp_makeConstraints { (make) in
            make.left.equalTo(3.5)
            make.bottom.equalTo(bottom)
            make.right.equalTo(-3.5)
            make.top.equalTo(9.0)
        }
        tableView.backgroundColor = UIColor.clearColor()
        self.tableView = tableView
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cellId")
        tableView.rowHeight = 44
    }
    private func createBottomView() -> Void {
        if self.dataSoureType == PopDataSoureType.OnlyHome {
            let bottomButton = UIButton(type: UIButtonType.Custom)
            self.view.addSubview(bottomButton)
            bottomButton.snp_makeConstraints(closure: { (make) in
                make.left.equalTo(5)
                make.bottom.equalTo(-10)
                make.right.equalTo(-5)
                make.top.equalTo(self.tableView!.snp_bottom).offset(3)
            })
            bottomButton.layer.cornerRadius = 3
            bottomButton.layer.masksToBounds = true
            bottomButton.layer.borderColor = RGB(0, g: 0, b: 0).CGColor
            bottomButton.layer.borderWidth = 0.5
            bottomButton.setTitle("编辑我的分组", forState: UIControlState.Normal)
            bottomButton.titleLabel?.font = UIFont.systemFontOfSize(14)
            bottomButton.addTarget(self, action: #selector(GYDPopViewController.hiddenSelf), forControlEvents: UIControlEvents.TouchUpInside)
        }
    }
    
    //#MARK: - UITableViewDataSource
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let tempDataSoure = self.dataSoure {
            return tempDataSoure.count
        }else {
            return 0
        }
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cellId", forIndexPath: indexPath)
        if let tempType = self.dataSoureType {
            if tempType == PopDataSoureType.OnlyHome {
                self.configHomeCell(cell, indexPath: indexPath)
            }else if tempType == PopDataSoureType.TitleAndImage {
                self.configImageTitleCell(cell, indexPath: indexPath)
            }
        }
        return cell
    }
    private func configHomeCell(cell: UITableViewCell , indexPath: NSIndexPath) -> Void {
        
        
        if let tempDataSoure = self.dataSoure {
            guard let title = tempDataSoure[indexPath.row] as? String else {
                return
            }
            cell.textLabel?.text = title
            cell.textLabel?.font = UIFont.systemFontOfSize(18)
        }
        
        if indexPath.row == self.selectedIdex {
            cell.textLabel?.textColor = RGB(254, g: 118, b: 1)
            cell.backgroundColor = RGB(150, g: 150, b: 150)
        }else {
            cell.textLabel?.textColor = UIColor.whiteColor()
            cell.backgroundColor = UIColor.clearColor()
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        
    }
    private func configImageTitleCell(cell: UITableViewCell , indexPath: NSIndexPath) -> Void {
        if let tempDataSoure = self.dataSoure {
            guard let data = tempDataSoure[indexPath.row] as? [String:String] else {
                return
            }
            cell.textLabel?.text = data["title"]
            cell.textLabel?.font = UIFont.systemFontOfSize(18)
            cell.textLabel?.textColor = UIColor.whiteColor()
            let imageName : String = data["imageName"]!
            cell.imageView?.image = UIImage(named: imageName)
            cell.backgroundColor = UIColor.clearColor()
            cell.selectionStyle = UITableViewCellSelectionStyle.None
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.removeFromSuperview()
        if let tempBlock = self.popSelectedBlock {
            tempBlock(index: indexPath.row)
        }
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    @objc private func hiddenSelf() -> Void {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    deinit {
        print("pop消失")
    }
}
